/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui2;

import entity.Manager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import management.ManagerBill;

/**
 *
 * @author Windows7
 */
public class GUIManagerBill extends javax.swing.JInternalFrame {
   Manager manager;
    /**
     * Creates new form GUIManagerBill
     */
    public GUIManagerBill( Manager manager) {
        try {
            Calendar now = Calendar.getInstance();
            String[] strDays = new String[]{
                "Sunday",
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thusday",
                "Friday",
                "Saturday"
            };
            initComponents();
             this.setResizable(false);
//             this.setTitle("Management bill");
            this.manager = manager;
            management.MyConnection.getConnection();
            management.GUIInteraction.readToTable(management.ManagerBill.sql_Bill_Bill_Detail, tblManagerBill);
            Date todayID = new Date(System.currentTimeMillis());
            SimpleDateFormat dayFomat = new SimpleDateFormat("yyyy-MM-dd");
            String today = dayFomat.format(todayID.getTime());
            lblValueDate.setText(today);
        } catch (SQLException ex) {
            Logger.getLogger(GUIManagerBill.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblManagerBill = new javax.swing.JTable();
        lblPay = new javax.swing.JLabel();
        btnEdit = new javax.swing.JButton();
        lblStatistics = new javax.swing.JLabel();
        rdoPaid = new javax.swing.JRadioButton();
        cbxStatistics = new javax.swing.JComboBox();
        lblDate = new javax.swing.JLabel();
        lblValueDate = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbxFillter = new javax.swing.JComboBox();
        btnView = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(1024, 650));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 0, 102));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/listBill.png"))); // NOI18N
        jLabel1.setText(" BILL MANAGEMENT");
        jLabel1.setPreferredSize(new java.awt.Dimension(150, 25));

        tblManagerBill.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblManagerBill.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblManagerBillMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblManagerBill);

        lblPay.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        lblPay.setText("Pay :");
        lblPay.setPreferredSize(new java.awt.Dimension(40, 25));

        btnEdit.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/edit.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.setPreferredSize(new java.awt.Dimension(130, 30));
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        lblStatistics.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        lblStatistics.setText("Statistics :");
        lblStatistics.setPreferredSize(new java.awt.Dimension(60, 25));

        rdoPaid.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        rdoPaid.setText("Paid");
        rdoPaid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoPaidActionPerformed(evt);
            }
        });

        cbxStatistics.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        cbxStatistics.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "By date", "By week", "By Status" }));
        cbxStatistics.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxStatisticsActionPerformed(evt);
            }
        });

        lblDate.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        lblDate.setText("Date :");
        lblDate.setPreferredSize(new java.awt.Dimension(50, 25));

        lblValueDate.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblValueDate.setPreferredSize(new java.awt.Dimension(100, 25));

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        jLabel2.setText("Fillter :");

        cbxFillter.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        cbxFillter.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bill is term", "Bill is overdue" }));
        cbxFillter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxFillterActionPerformed(evt);
            }
        });

        btnView.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        btnView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/mnuInfo.png"))); // NOI18N
        btnView.setText("View all");
        btnView.setPreferredSize(new java.awt.Dimension(90, 30));
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(98, 98, 98)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(44, 44, 44)
                .addComponent(cbxFillter, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblStatistics, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbxStatistics, 0, 141, Short.MAX_VALUE)
                    .addComponent(lblValueDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(108, 108, 108))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 685, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(lblPay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(rdoPaid, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(293, 293, 293)
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(122, 122, 122)
                        .addComponent(btnView, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(189, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblValueDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblStatistics, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxStatistics, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(cbxFillter, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rdoPaid))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnView, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(850, 850, 850))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblManagerBillMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblManagerBillMouseClicked
        // TODO add your handling code here:
        int i = tblManagerBill.getSelectedRow();
        if (i >= 0) {
            rdoPaid.setSelected(Boolean.valueOf(tblManagerBill.getValueAt(i, 3).toString()));

        }
    }//GEN-LAST:event_tblManagerBillMouseClicked

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        int selectRow = tblManagerBill.getSelectedRow();
        if (selectRow == -1) {
            JOptionPane.showMessageDialog(this, "Don't choice Bill edit !");
        }
        //manager.getId();
        // idF lay ra gia tri cua id trong dong ma minh chon.
        int idF = Integer.parseInt(tblManagerBill.getModel().getValueAt(selectRow, 0).toString());
        if (rdoPaid.isSelected()) {
            try {
                int status = 1;              
                management.ManagerBill.updateBill(manager.getId(),status, idF);
                management.GUIInteraction.readToTable(ManagerBill.sql_Bill_Bill_Detail, tblManagerBill);
                JOptionPane.showMessageDialog(this, "Successfull !");
            } catch (SQLException ex) {
                Logger.getLogger(GUIManagerBill.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        } else {
            JOptionPane.showMessageDialog(this, "Don't Successfull !");
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void cbxStatisticsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxStatisticsActionPerformed
        // TODO add your handling code here:
        int searchBY = cbxStatistics.getSelectedIndex();
        if (searchBY == 0) {
            try {
                management.GUIInteraction.readToTable("select Bill_Id, Staff_Id, Customer_Id,Total_Money, Create_Date, Pay from Bill where('" + java.sql.Date.valueOf(lblValueDate.getText()) + "' - Create_Date) = 1", tblManagerBill);
            } catch (SQLException ex) {
                Logger.getLogger(GUIManagerBill.class.getName()).log(Level.SEVERE, null, ex);
            }
           
        }
        if (searchBY == 1) {
            try {
                management.GUIInteraction.readToTable("select Bill_Id, Staff_Id, Customer_Id,Total_Money,Create_Date, Pay from Bill where('" + java.sql.Date.valueOf(lblValueDate.getText()) + "' - Create_Date) <= 7", tblManagerBill);
            } catch (SQLException ex) {
                Logger.getLogger(GUIManagerBill.class.getName()).log(Level.SEVERE, null, ex);
            }
           
        }
        if (searchBY == 2) {
            try {
                management.GUIInteraction.readToTable("select Bill_Detail.Bill_Id, Staff_Id, Customer_Id,Total_Money, Create_Date, Pay from Bill inner join Bill_Detail on Bill.Bill_Id = Bill_Detail.Bill_Id where Pay = 0", tblManagerBill);
            } catch (SQLException ex) {
                Logger.getLogger(GUIManagerBill.class.getName()).log(Level.SEVERE, null, ex);
            }
          
        }
    }//GEN-LAST:event_cbxStatisticsActionPerformed

    private void rdoPaidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoPaidActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdoPaidActionPerformed

    private void cbxFillterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxFillterActionPerformed
        // TODO add your handling code here:
        int id = cbxFillter.getSelectedIndex();
        if (id == 0) {
            try {
                management.GUIInteraction.readToTable("select * from Bill where ('" + lblValueDate.getText() + "' - Create_Date = 30) and Pay = 0 ", tblManagerBill);
            } catch (SQLException ex) {
                Logger.getLogger(GUIManagerBill.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (id == 1) {
            try {
                management.GUIInteraction.readToTable("select * from Bill where ('" + lblValueDate.getText() + "'-Create_Date >30 and Pay = 0 )", tblManagerBill);
            } catch (SQLException ex) {
                Logger.getLogger(GUIManagerBill.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }//GEN-LAST:event_cbxFillterActionPerformed

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            management.GUIInteraction.readToTable(management.ManagerBill.sql_Bill_Bill_Detail, tblManagerBill);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnViewActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnView;
    private javax.swing.JComboBox cbxFillter;
    private javax.swing.JComboBox cbxStatistics;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblPay;
    private javax.swing.JLabel lblStatistics;
    private javax.swing.JLabel lblValueDate;
    private javax.swing.JRadioButton rdoPaid;
    private javax.swing.JTable tblManagerBill;
    // End of variables declaration//GEN-END:variables
}
